<?php

namespace app\hotel\controller;

use Admin\Model\RootModel;
use app\hotel\model\Room as RoomModel;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Room extends Common
{

    //房间分类主页
    public function roomKinds()
    {

        if (Request::instance()->isAjax()) {

            $where = ['t1.is_del' => 1];
            $data = RoomModel::getRoomKindsList($where);

            return ['code' => 0, 'msg' => '', 'data' => $data];

        } else {
            $dataName = RoomModel::getOlnyDataNameList(); 
            $this->assign('room_kinds', $dataName);
            return view();
        }

    }

    public function addRoomKinds()
    {

        if (Request::instance()->isAjax()) {

            $post = Request::instance()->post();
            $post = $post['data'];
            $room = [];
            $pro = [];
            $catering = [];
           // $keys = array_keys($post); 
                foreach ($post as $key => $value) {

                   if(substr($key, 0,2)=='1-'){
                           if($value)
                           $room[] = $value; 
                   }
                   if(substr($key, 0,2)=='2-'){
                          if($value)
                           $pro[] = $value; 
                   }
                    if(substr($key, 0,2)=='3-'){
                          if($value)
                           $catering[] = $value; 
                   }} 
                    $room = implode(",", $room); 
                    $pro = implode(",", $pro); 
                    $catering = implode(",", $catering);  
                $addData = [
                'hotel_id' => Session::get('hotel_id'),
                'room_name' => $post['room_name'],
                'room_price' => $post['room_price'],
                'is_tehui' => $post['is_tehui'],
                'room_discount_price' => $post['room_discount_price'],
                 'is_quxiao' => $post['is_quxiao'],
                'square' => $post['square'],
                'available_person' => $post['available_person'],
                'bed_scale' => $post['bed_scale'],
                'room_fw_other' => $room,
                'room_other'=> $pro,
                'food_other'=>$catering, 
                'room_images' => 0 ,
                'room_count' => $post['room_count'],
                'create_time' => date('Y-m-d H:i:s'),
                'cancelable_time' => $post['cancelable_time']
            ];

            $res = Db::name('hotel_room')->insertGetId($addData);

            if ($res) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }

    }

    public function editRoomKinds()
    {

        if (Request::instance()->isAjax()) {
            $room = [];
            $pro = [];
            $catering = [];
            $post = Request::instance()->post();
            $post = $post['data'];
            foreach ($post as $key => $value) {

                   if(substr($key, 0,2)=='1-'){
                           if($value)
                           $room[] = $value; 
                   }
                   if(substr($key, 0,2)=='2-'){
                          if($value)
                           $pro[] = $value; 
                   }
                    if(substr($key, 0,2)=='3-'){
                          if($value)
                           $catering[] = $value; 
                   }} 
                    $room = implode(",", $room); 
                    $pro = implode(",", $pro); 
                    $catering = implode(",", $catering);     
            $editData = [
                'room_name' => $post['room_name'],
                'room_price' => $post['room_price'],
                'is_tehui' => $post['is_tehui'],
                'room_discount_price' => $post['room_discount_price'],
                'is_quxiao' => $post['is_quxiao'],
                'square' => $post['square'],
                'available_person' => $post['available_person'],
                'bed_scale' => $post['bed_scale'],
                'room_fw_other' => $room,
                'room_other'=> $pro,
                'room_count'=> $post['room_count'],
                'food_other'=>$catering,  
                'cancelable_time' => $post['cancelable_time']
            ];

            $res = RoomModel::editRoomKinds($post['room_id'], $editData);

            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }

    }

    public function editRoomKindsDisplay()
    {

        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();

            if ($post['is_display'] == 1) {
                //上架最少上传一张图片
                $albumCount = RoomModel::getRoomImgList($post['room_id'], true);
                if ($albumCount == 0) {
                    return ['code' => 1, 'msg' => '请至少上传一张房间图片'];
                    exit;
                }
            } 
            $res = RoomModel::editRoomKinds($post['room_id'], ['is_display' => $post['is_display']]); 
            if ($res) {
                return ['code' => 1, 'msg' => '修改成功'];
            }

        }

    }

    //房间管理主页
    public function room()
    {
        if (Request::instance()->isAjax()) {

            $get = Request::instance()->get();

            $where['t1.is_del'] = 0;

            if (isset($get['room_id']) && $get['room_id'] != '') {
                $where['t1.room_id'] = array('eq', $get['room_id']);
            }
             if (isset($get['is_ruzhu']) && $get['is_ruzhu'] != '') {
                $where['t1.is_ruzhu'] = array('eq', $get['is_ruzhu']);
            }

            $count = RoomModel::getRoomList($where, 0, 0, true);

            $pagesize = $get['limit'];
            $page = (int)$get['page'];
            $startrow = ($page - 1) * $pagesize;

            $data = RoomModel::getRoomList($where, $page, $startrow, false); 
            return ['code' => 0, 'msg' => '', 'count' => $count, 'data' => $data];

        } else {

            $room_kinds = RoomModel::getOlnyRoomKindsList('room_id,room_name');
            $this->assign('room_kinds', $room_kinds);
            return view();
        }

    }

    //添加房间
    public function addRoom()
    {

        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();

            $addData = [
                'room_id' => $post['data']['room_id'],
                'room_number' => $post['data']['room_number'],
                'remarks' => $post['data']['remarks']
            ];
            $res = Db::name('hotel_eroom')->insert($addData);

            if ($res) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }

    }

    //修改房间
    public function editRoom()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();

            $editData = [
                'room_id' => $post['data']['room_id'],
                'room_number' => $post['data']['room_number'],
                'remarks' => $post['data']['remarks'],
                'is_ruzhu' => $post['data']['is_ruzhu']
            ]; 
            $res = RoomModel::editRoom($post['data']['eroom_id'], $editData);

            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }
    }

    //删除房间
    public function delRoom()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();

            $editData = ['is_del'=>1];

            //$res = RoomModel::editRoom($post['eroom_id'], $editData);
             $res = RoomModel::delRoom($post['room_id']);
            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }

        }
    }
 

}